<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>StdIn.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="StdIn code in Java">
<meta NAME="TITLE" CONTENT="StdIn code in Java">
<meta NAME="KEYWORDS" CONTENT="StdIn,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>StdIn.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "StdIn.java">StdIn.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac StdIn.java</span>
<span class="comment"> *  Execution:    java StdIn   (interactive test of basic functionality)</span>
<span class="comment"> *  Dependencies: none</span>
<span class="comment"> *</span>
<span class="comment"> *  Reads in data of various types from standard input.</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">ArrayList</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">InputMismatchException</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">Locale</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">NoSuchElementException</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">Scanner</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">regex</span><span class="symbol">.</span><span class="normal">Pattern</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">StdIn</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class provides static methods for reading strings</span>
<span class="comment"> *  and numbers from standard input.</span>
<span class="comment"> *  These functions fall into one of four categories:</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment">those for reading individual tokens from standard input, one at a time,</span>
<span class="comment"> *      and converting each to a number, string, or boolean</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment">those for reading characters from standard input, one at a time</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment">those for reading lines from standard input, one at a time</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment">those for reading a sequence of values of the same type from standard input,</span>
<span class="comment"> *      and returning the values in an array</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  Generally, it is best not to mix functions from the different</span>
<span class="comment"> *  categories in the same program. </span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Reading tokens from standard input one at a time,</span>
<span class="comment"> *  and converting to numbers and strings.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  You can use the following methods to read numbers, strings, and booleans</span>
<span class="comment"> *  from standard input:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #readInt()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #readDouble()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #readString()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #readBoolean()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #readShort()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #readLong()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #readFloat()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #readByte()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  Each method skips over any input that is whitespace. Then, it reads</span>
<span class="comment"> *  the next token and attempts to convert it into a value of the specified</span>
<span class="comment"> *  type. If it succeeds, it returns that value; otherwise, it</span>
<span class="comment"> *  throws a {</span><span class="type">@link</span><span class="comment"> InputMismatchException}.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">Whitespace</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> includes spaces, tabs, and newlines; the full definition</span>
<span class="comment"> *  is inherited from {</span><span class="type">@link</span><span class="comment"> Character#isWhitespace(char)}.</span>
<span class="comment"> *  A </span><span class="keyword">&lt;em&gt;</span><span class="comment">token</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is a maximal sequence of non-whitespace characters.</span>
<span class="comment"> *  The precise rules for describing which tokens can be converted to</span>
<span class="comment"> *  integers and floating-point numbers are inherited from</span>
<span class="comment"> *  </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="normal"> </span><span class="symbol">=</span><span class="string"> "http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#number-syntax"</span><span class="keyword">&gt;</span><span class="comment">Scanner</span><span class="keyword">&lt;/a&gt;</span><span class="comment">,</span>
<span class="comment"> *  using the locale {</span><span class="type">@link</span><span class="comment"> Locale#US}; the rules</span>
<span class="comment"> *  for floating-point numbers are slightly different</span>
<span class="comment"> *  from those in {</span><span class="type">@link</span><span class="comment"> Double#valueOf(String)},</span>
<span class="comment"> *  but unlikely to be of concern to most programmers.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Reading characters from standard input, one at a time.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  You can use the following two methods to read characters from standard input:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #hasNextChar()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #readChar()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The first method returns true if standard input has more input (including whitespace).</span>
<span class="comment"> *  The second method reads and returns the next character of input on standard </span>
<span class="comment"> *  input (possibly a whitespace character).</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  As an example, the following code fragment reads characters from standard input,</span>
<span class="comment"> *  one character at a time, and prints it to standard output.</span>
<span class="comment"> *  </span><span class="keyword">&lt;pre&gt;</span>
<span class="comment"> *  while (!StdIn.hasNextChar()) {</span>
<span class="comment"> *      char c = StdIn.readChar();</span>
<span class="comment"> *      StdOut.print(c);</span>
<span class="comment"> *  }</span>
<span class="comment"> *  </span><span class="keyword">&lt;/pre&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Reading lines from standard input, one at a time.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  You can use the following two methods to read lines from standard input:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #hasNextLine()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #readLine()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The first method returns true if standard input has more input (including whitespace).</span>
<span class="comment"> *  The second method reads and returns the remaining portion of </span>
<span class="comment"> *  the next line of input on standard input (possibly whitespace),</span>
<span class="comment"> *  discarding the trailing line separator.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  A </span><span class="keyword">&lt;em&gt;</span><span class="comment">line separator</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is defined to be one of the following strings:</span>
<span class="comment"> *  {</span><span class="type">@code</span><span class="comment"> \n} (Linux), {</span><span class="type">@code</span><span class="comment"> \r} (old Macintosh),</span>
<span class="comment"> *  {</span><span class="type">@code</span><span class="comment"> \r\n} (Windows),</span>
<span class="comment"> *  </span><span class="keyword">&lt;code&gt;</span><span class="comment">&amp;#92;u2028</span><span class="keyword">&lt;/code&gt;</span><span class="comment">, </span><span class="keyword">&lt;code&gt;</span><span class="comment">&amp;#92;u2029</span><span class="keyword">&lt;/code&gt;</span><span class="comment">, or </span><span class="keyword">&lt;code&gt;</span><span class="comment">&amp;#92;u0085</span><span class="keyword">&lt;/code&gt;</span><span class="comment">.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  As an example, the following code fragment reads text from standard input,</span>
<span class="comment"> *  one line at a time, and prints it to standard output.</span>
<span class="comment"> *  </span><span class="keyword">&lt;pre&gt;</span>
<span class="comment"> *  while (!StdIn.hasNextLine()) {</span>
<span class="comment"> *      String line = StdIn.readLine();</span>
<span class="comment"> *      StdOut.println(line);</span>
<span class="comment"> *  }</span>
<span class="comment"> *  </span><span class="keyword">&lt;/pre&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Reading a sequence of values of the same type from standard input.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  You can use the following methods to read a sequence numbers, strings,</span>
<span class="comment"> *  or booleans (all of the same type) from standard input:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #readAllDoubles()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #readAllInts()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #readAllStrings()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #readAllLines()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #readAll()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The first three methods read of all of remaining token on standard input</span>
<span class="comment"> *  and dconverts the tokens to values of</span>
<span class="comment"> *  the specified type, as in the corresponding</span>
<span class="comment"> *  {</span><span class="type">@code</span><span class="comment"> readDouble}, {</span><span class="type">@code</span><span class="comment"> readInt}, and {</span><span class="type">@code</span><span class="comment"> readString()} methods.</span>
<span class="comment"> *  The {</span><span class="type">@code</span><span class="comment"> readAllLines()} method reads all remaining lines on standard</span>
<span class="comment"> *  input and returns them as an array of strings.</span>
<span class="comment"> *  The {</span><span class="type">@code</span><span class="comment"> readAll()} method reads all remaining input on standard</span>
<span class="comment"> *  input and returns it as a string.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  As an example, the following code fragment reads all of the remaining</span>
<span class="comment"> *  tokens from standard input and returns them as an array of strings.</span>
<span class="comment"> *  </span><span class="keyword">&lt;pre&gt;</span>
<span class="comment"> *  String[] words = StdIn.readAllString();</span>
<span class="comment"> *  </span><span class="keyword">&lt;/pre&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Differences with Scanner.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  {</span><span class="type">@code</span><span class="comment"> StdIn} and {</span><span class="type">@link</span><span class="comment"> Scanner} are both designed to parse </span>
<span class="comment"> *  tokens and convert them to primitive types and strings.</span>
<span class="comment"> *  Some of the main differences are summarized below:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@code</span><span class="comment"> StdIn} is a set of static methods and reads </span>
<span class="comment"> *       reads input from only standard input. It is suitable for use before</span>
<span class="comment"> *       a programmer knows about objects.</span>
<span class="comment"> *       See {</span><span class="type">@link</span><span class="comment"> In} for an object-oriented version that handles</span>
<span class="comment"> *       input from files, URLs,</span>
<span class="comment"> *       and sockets.</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@code</span><span class="comment"> StdIn} uses whitespace as the delimiter between tokens.</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@code</span><span class="comment"> StdIn} coerces the character-set encoding to UTF-8,</span>
<span class="comment"> *       which is a standard character encoding for Unicode.</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@code</span><span class="comment"> StdIn} coerces the locale to {</span><span class="type">@link</span><span class="comment"> Locale#US},</span>
<span class="comment"> *       for consistency with {</span><span class="type">@link</span><span class="comment"> StdOut}, {</span><span class="type">@link</span><span class="comment"> Double#parseDouble(String)},</span>
<span class="comment"> *       and floating-point literals.</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@code</span><span class="comment"> StdIn} has convenient methods for reading a single</span>
<span class="comment"> *       character; reading in sequences of integers, doubles, or strings;</span>
<span class="comment"> *       and reading in all of the remaining input.</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  Historical note: {</span><span class="type">@code</span><span class="comment"> StdIn} preceded {</span><span class="type">@code</span><span class="comment"> Scanner}; when</span>
<span class="comment"> *  {</span><span class="type">@code</span><span class="comment"> Scanner} was introduced, this class was reimplemented to use it.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Using standard input.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  Standard input is fundamental operating system abstraction, on Mac OS X,</span>
<span class="comment"> *  Windows, and Linux.</span>
<span class="comment"> *  The methods in {</span><span class="type">@code</span><span class="comment"> StdIn} are </span><span class="keyword">&lt;em&gt;</span><span class="comment">blocking</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, which means that they</span>
<span class="comment"> *  will wait until you enter input on standard input.</span>
<span class="comment"> *  If your program has a loop that repeats until standard input is empty,</span>
<span class="comment"> *  you must signal that the input is finished.</span>
<span class="comment"> *  To do so, depending on your operating system and IDE,</span>
<span class="comment"> *  use either {</span><span class="type">@code</span><span class="comment"> </span><span class="keyword">&lt;Ctrl-d&gt;</span><span class="comment">} or {</span><span class="type">@code</span><span class="comment"> </span><span class="keyword">&lt;Ctrl-z&gt;</span><span class="comment">}, on its own line.</span>
<span class="comment"> *  If you are redirecting standard input from a file, you will not need</span>
<span class="comment"> *  to do anything to signal that the input is finished.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Known bugs.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  Java's UTF-8 encoding does not recognize the optional </span>
<span class="comment"> *  </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="normal"> </span><span class="symbol">=</span><span class="string"> "http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058"</span><span class="keyword">&gt;</span><span class="comment">byte-order mask</span><span class="keyword">&lt;/a&gt;</span><span class="comment">.</span>
<span class="comment"> *  If the input begins with the optional byte-order mask, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">StdIn</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment"> *  will have an extra character </span><span class="keyword">&lt;code&gt;</span><span class="comment">&amp;#92;uFEFF</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> at the beginning.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Reference.</span><span class="keyword">&lt;/b&gt;</span><span class="comment"> </span>
<span class="comment"> *  For additional documentation,</span>
<span class="comment"> *  see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://introcs.cs.princeton.edu/15inout"</span><span class="keyword">&gt;</span><span class="comment">Section 1.5</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of   </span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">Introduction to Programming in Java: An Interdisciplinary Approach</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment"> *  by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> David Pritchard</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">StdIn</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">    </span><span class="comment">/*** begin: section (1 of 2) of code duplicated from In to StdIn. */</span>
<span class="normal">    </span>
<span class="normal">    </span><span class="comment">// assume Unicode UTF-8 encoding</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> CHARSET_NAME </span><span class="symbol">=</span><span class="normal"> </span><span class="string">"UTF-8"</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// assume language = English, country = US for consistency with System.out.</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Locale</span><span class="normal"> LOCALE </span><span class="symbol">=</span><span class="normal"> Locale</span><span class="symbol">.</span><span class="normal">US</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// the default token separator; we maintain the invariant that this value</span>
<span class="normal">    </span><span class="comment">// is held by the scanner's delimiter between calls</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Pattern</span><span class="normal"> WHITESPACE_PATTERN </span><span class="symbol">=</span><span class="normal"> Pattern</span><span class="symbol">.</span><span class="function">compile</span><span class="symbol">(</span><span class="string">"</span><span class="specialchar">\\</span><span class="string">p{javaWhitespace}+"</span><span class="symbol">);</span>

<span class="normal">    </span><span class="comment">// makes whitespace significant</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Pattern</span><span class="normal"> EMPTY_PATTERN </span><span class="symbol">=</span><span class="normal"> Pattern</span><span class="symbol">.</span><span class="function">compile</span><span class="symbol">(</span><span class="string">""</span><span class="symbol">);</span>

<span class="normal">    </span><span class="comment">// used to read the entire input</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Pattern</span><span class="normal"> EVERYTHING_PATTERN </span><span class="symbol">=</span><span class="normal"> Pattern</span><span class="symbol">.</span><span class="function">compile</span><span class="symbol">(</span><span class="string">"</span><span class="specialchar">\\</span><span class="string">A"</span><span class="symbol">);</span>

<span class="normal">    </span><span class="comment">/*** end: section (1 of 2) of code duplicated from In to StdIn. */</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Scanner</span><span class="normal"> scanner</span><span class="symbol">;</span>
<span class="normal"> </span>
<span class="normal">    </span><span class="comment">// it doesn't make sense to instantiate this class</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="function">StdIn</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">//// begin: section (2 of 2) of code duplicated from In to StdIn,</span>
<span class="normal">    </span><span class="comment">//// with all methods changed from "public" to "public static"</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Returns true if standard input is empty (except possibly for whitespace).</span>
<span class="comment">     * Use this method to know whether the next call to {</span><span class="type">@link</span><span class="comment"> #readString()}, </span>
<span class="comment">     * {</span><span class="type">@link</span><span class="comment"> #readDouble()}, etc will succeed.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if standard input is empty (except possibly</span>
<span class="comment">     *         for whitespace); </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">isEmpty</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">!</span><span class="normal">scanner</span><span class="symbol">.</span><span class="function">hasNext</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Returns true if standard input has a next line.</span>
<span class="comment">     * Use this method to know whether the</span>
<span class="comment">     * next call to {</span><span class="type">@link</span><span class="comment"> #readLine()} will succeed.</span>
<span class="comment">     * This method is functionally equivalent to {</span><span class="type">@link</span><span class="comment"> #hasNextChar()}.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if standard input is empty;</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">hasNextLine</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">hasNextLine</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns true if standard input has more inputy (including whitespace).</span>
<span class="comment">     * Use this method to know whether the next call to {</span><span class="type">@link</span><span class="comment"> #readChar()} will succeed.</span>
<span class="comment">     * This method is functionally equivalent to {</span><span class="type">@link</span><span class="comment"> #hasNextLine()}.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if standard input has more input (including whitespace);</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">hasNextChar</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        scanner</span><span class="symbol">.</span><span class="function">useDelimiter</span><span class="symbol">(</span><span class="normal">EMPTY_PATTERN</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">boolean</span><span class="normal"> result </span><span class="symbol">=</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">hasNext</span><span class="symbol">();</span>
<span class="normal">        scanner</span><span class="symbol">.</span><span class="function">useDelimiter</span><span class="symbol">(</span><span class="normal">WHITESPACE_PATTERN</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> result</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads and returns the next line, excluding the line separator if present.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next line, excluding the line separator if present</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> </span><span class="function">readLine</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> line</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            line </span><span class="symbol">=</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">nextLine</span><span class="symbol">();</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">NoSuchElementException</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            line </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> line</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads and returns the next character.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next character</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">char</span><span class="normal"> </span><span class="function">readChar</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        scanner</span><span class="symbol">.</span><span class="function">useDelimiter</span><span class="symbol">(</span><span class="normal">EMPTY_PATTERN</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> ch </span><span class="symbol">=</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">next</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">assert</span><span class="normal"> ch</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">:</span><span class="normal"> </span><span class="string">"Internal (Std)In.readChar() error!"</span>
<span class="normal">            </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" Please contact the authors."</span><span class="symbol">;</span>
<span class="normal">        scanner</span><span class="symbol">.</span><span class="function">useDelimiter</span><span class="symbol">(</span><span class="normal">WHITESPACE_PATTERN</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> ch</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="number">0</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span><span class="normal">  </span>


<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads and returns the remainder of the input, as a string.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the remainder of the input, as a string</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> </span><span class="function">readAll</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">scanner</span><span class="symbol">.</span><span class="function">hasNextLine</span><span class="symbol">())</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> </span><span class="string">""</span><span class="symbol">;</span>

<span class="normal">        </span><span class="usertype">String</span><span class="normal"> result </span><span class="symbol">=</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">useDelimiter</span><span class="symbol">(</span><span class="normal">EVERYTHING_PATTERN</span><span class="symbol">).</span><span class="function">next</span><span class="symbol">();</span>
<span class="normal">        </span><span class="comment">// not that important to reset delimeter, since now scanner is empty</span>
<span class="normal">        scanner</span><span class="symbol">.</span><span class="function">useDelimiter</span><span class="symbol">(</span><span class="normal">WHITESPACE_PATTERN</span><span class="symbol">);</span><span class="normal"> </span><span class="comment">// but let's do it anyway</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> result</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads the next token  and returns the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">String</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next </span><span class="keyword">&lt;tt&gt;</span><span class="comment">String</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> </span><span class="function">readString</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">next</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads the next token from standard input, parses it as an integer, and returns the integer.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next integer on standard input</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> InputMismatchException if the next token cannot be parsed as an </span><span class="keyword">&lt;tt&gt;</span><span class="comment">int</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">readInt</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">nextInt</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads the next token from standard input, parses it as a double, and returns the double.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next double on standard input</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> InputMismatchException if the next token cannot be parsed as a </span><span class="keyword">&lt;tt&gt;</span><span class="comment">double</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">readDouble</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">nextDouble</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads the next token from standard input, parses it as a float, and returns the float.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next float on standard input</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> InputMismatchException if the next token cannot be parsed as a </span><span class="keyword">&lt;tt&gt;</span><span class="comment">float</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">float</span><span class="normal"> </span><span class="function">readFloat</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">nextFloat</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads the next token from standard input, parses it as a long integer, and returns the long integer.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next long integer on standard input</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> InputMismatchException if the next token cannot be parsed as a </span><span class="keyword">&lt;tt&gt;</span><span class="comment">long</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">long</span><span class="normal"> </span><span class="function">readLong</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">nextLong</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads the next token from standard input, parses it as a short integer, and returns the short integer.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next short integer on standard input</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> InputMismatchException if the next token cannot be parsed as a </span><span class="keyword">&lt;tt&gt;</span><span class="comment">short</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">short</span><span class="normal"> </span><span class="function">readShort</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">nextShort</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads the next token from standard input, parses it as a byte, and returns the byte.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next byte on standard input</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> InputMismatchException if the next token cannot be parsed as a </span><span class="keyword">&lt;tt&gt;</span><span class="comment">byte</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">byte</span><span class="normal"> </span><span class="function">readByte</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">nextByte</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads the next token from standard input, parses it as a boolean,</span>
<span class="comment">     * and returns the boolean.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next boolean on standard input</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> InputMismatchException if the next token cannot be parsed as a </span><span class="keyword">&lt;tt&gt;</span><span class="comment">boolean</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">:</span>
<span class="comment">     *    </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> or </span><span class="keyword">&lt;tt&gt;</span><span class="comment">1</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> for true, and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> or </span><span class="keyword">&lt;tt&gt;</span><span class="comment">0</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> for false,</span>
<span class="comment">     *    ignoring case</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">readBoolean</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> </span><span class="function">readString</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">.</span><span class="function">equalsIgnoreCase</span><span class="symbol">(</span><span class="string">"true"</span><span class="symbol">))</span><span class="normal">  </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">.</span><span class="function">equalsIgnoreCase</span><span class="symbol">(</span><span class="string">"false"</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">.</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"1"</span><span class="symbol">))</span><span class="normal">               </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">.</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"0"</span><span class="symbol">))</span><span class="normal">               </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">InputMismatchException</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads all remaining tokens from standard input and returns them as an array of strings.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> all remaining tokens on standard input, as an array of strings</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> String</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">readAllStrings</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="comment">// we could use readAll.trim().split(), but that's not consistent</span>
<span class="normal">        </span><span class="comment">// because trim() uses characters 0x00..0x20 as whitespace</span>
<span class="normal">        String</span><span class="symbol">[]</span><span class="normal"> tokens </span><span class="symbol">=</span><span class="normal"> WHITESPACE_PATTERN</span><span class="symbol">.</span><span class="function">split</span><span class="symbol">(</span><span class="function">readAll</span><span class="symbol">());</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">tokens</span><span class="symbol">.</span><span class="normal">length </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> tokens</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">].</span><span class="function">length</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> tokens</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// don't include first token if it is leading whitespace</span>
<span class="normal">        String</span><span class="symbol">[]</span><span class="normal"> decapitokens </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> String</span><span class="symbol">[</span><span class="normal">tokens</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> tokens</span><span class="symbol">.</span><span class="normal">length </span><span class="symbol">-</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            decapitokens</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> tokens</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> decapitokens</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads all remaining lines from standard input and returns them as an array of strings.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> all remaining lines on standard input, as an array of strings</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> String</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">readAllLines</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">ArrayList&lt;String&gt;</span><span class="normal"> lines </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> ArrayList</span><span class="symbol">&lt;</span><span class="normal">String</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="function">hasNextLine</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            lines</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="function">readLine</span><span class="symbol">());</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> lines</span><span class="symbol">.</span><span class="function">toArray</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> String</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads all remaining tokens from standard input, parses them as integers, and returns</span>
<span class="comment">     * them as an array of integers.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> all remaining integers on standard input, as an array</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> InputMismatchException if any token cannot be parsed as an </span><span class="keyword">&lt;tt&gt;</span><span class="comment">int</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">readAllInts</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        String</span><span class="symbol">[]</span><span class="normal"> fields </span><span class="symbol">=</span><span class="normal"> </span><span class="function">readAllStrings</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> vals </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">fields</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> fields</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            vals</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">fields</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> vals</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads all remaining tokens from standard input, parses them as doubles, and returns</span>
<span class="comment">     * them as an array of doubles.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> all remaining doubles on standard input, as an array</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> InputMismatchException if any token cannot be parsed as a </span><span class="keyword">&lt;tt&gt;</span><span class="comment">double</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">readAllDoubles</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        String</span><span class="symbol">[]</span><span class="normal"> fields </span><span class="symbol">=</span><span class="normal"> </span><span class="function">readAllStrings</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> vals </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">double</span><span class="symbol">[</span><span class="normal">fields</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> fields</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            vals</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> Double</span><span class="symbol">.</span><span class="function">parseDouble</span><span class="symbol">(</span><span class="normal">fields</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> vals</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="normal">    </span>
<span class="normal">    </span><span class="comment">//// end: section (2 of 2) of code duplicated from In to StdIn</span>
<span class="normal">    </span>
<span class="normal">    </span>
<span class="normal">    </span><span class="comment">// do this once when StdIn is initialized</span>
<span class="normal">    </span><span class="keyword">static</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">resync</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * If StdIn changes, use this to reinitialize the scanner.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">resync</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">setScanner</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> </span><span class="function">Scanner</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">io</span><span class="symbol">.</span><span class="function">BufferedInputStream</span><span class="symbol">(</span><span class="normal">System</span><span class="symbol">.</span><span class="normal">in</span><span class="symbol">),</span><span class="normal"> CHARSET_NAME</span><span class="symbol">));</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="normal">    </span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setScanner</span><span class="symbol">(</span><span class="usertype">Scanner</span><span class="normal"> scanner</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        StdIn</span><span class="symbol">.</span><span class="normal">scanner </span><span class="symbol">=</span><span class="normal"> scanner</span><span class="symbol">;</span>
<span class="normal">        StdIn</span><span class="symbol">.</span><span class="normal">scanner</span><span class="symbol">.</span><span class="function">useLocale</span><span class="symbol">(</span><span class="normal">LOCALE</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads all remaining tokens, parses them as integers, and returns</span>
<span class="comment">     * them as an array of integers.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> all remaining integers, as an array</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> InputMismatchException if any token cannot be parsed as an </span><span class="keyword">&lt;tt&gt;</span><span class="comment">int</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@deprecated</span><span class="comment"> Replaced by {</span><span class="type">@link</span><span class="comment"> #readAllInts()}.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">readInts</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">readAllInts</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads all remaining tokens, parses them as doubles, and returns</span>
<span class="comment">     * them as an array of doubles.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> all remaining doubles, as an array</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> InputMismatchException if any token cannot be parsed as a </span><span class="keyword">&lt;tt&gt;</span><span class="comment">double</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@deprecated</span><span class="comment"> Replaced by {</span><span class="type">@link</span><span class="comment"> #readAllDoubles()}.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">readDoubles</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">readAllDoubles</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads all remaining tokens and returns them as an array of strings.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> all remaining tokens, as an array of strings</span>
<span class="comment">     * </span><span class="type">@deprecated</span><span class="comment"> Replaced by {</span><span class="type">@link</span><span class="comment"> #readAllStrings()}.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> String</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">readStrings</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">readAllStrings</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Interactive test of basic functionality.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="string">"Type a string: "</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> StdIn</span><span class="symbol">.</span><span class="function">readString</span><span class="symbol">();</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Your string was: "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> s</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="string">"Type an int: "</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> a </span><span class="symbol">=</span><span class="normal"> StdIn</span><span class="symbol">.</span><span class="function">readInt</span><span class="symbol">();</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Your int was: "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> a</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="string">"Type a boolean: "</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">boolean</span><span class="normal"> b </span><span class="symbol">=</span><span class="normal"> StdIn</span><span class="symbol">.</span><span class="function">readBoolean</span><span class="symbol">();</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Your boolean was: "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> b</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="string">"Type a double: "</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> c </span><span class="symbol">=</span><span class="normal"> StdIn</span><span class="symbol">.</span><span class="function">readDouble</span><span class="symbol">();</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Your double was: "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> c</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Sat Oct 24 19:31:17 EDT 2015.
</small></address>

</html>
